package com.ocean.courtyard.command;

import com.ocean.courtyard.constants.ConstantsAttribute;
import com.ocean.courtyard.core.datasource.SqlSessionImpl;
import com.ocean.courtyard.core.hook.HuskHomeApiHook;
import com.ocean.courtyard.core.worldguard.WorldGuardSetting;
import com.ocean.courtyard.message.Message;
import com.onarandombox.MultiverseCore.MultiverseCore;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

public class MasterCommand implements CommandExecutor {

    private final JavaPlugin plugin;

    private final MultiverseCore core;

    private final SqlSessionImpl sqlSession;

    private final Message message;

    private final HuskHomeApiHook huskHomesAPIHook;

    private final WorldGuardSetting worldGuardSetting;

    public MasterCommand(JavaPlugin plugin, SqlSessionImpl sqlSession, Message message, HuskHomeApiHook huskHomesAPIHook, WorldGuardSetting integrate) {
        this.plugin = plugin;
        this.core = JavaPlugin.getPlugin(MultiverseCore.class);
        this.sqlSession = sqlSession;
        this.message = message;
        this.huskHomesAPIHook = huskHomesAPIHook;
        this.worldGuardSetting = integrate;
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (!command.getName().equalsIgnoreCase("cy")) {
            return false;
        }
        if (sender instanceof Player player) {
            //开启了只使用传送功能，只匹配传送命令
            boolean justTransmission = plugin.getConfig().getBoolean(ConstantsAttribute.JUST_TRANSMISSION);
            if (Boolean.TRUE.equals(justTransmission)) {
                if (args.length == 1 && args[0].equalsIgnoreCase("home")) {
                    return new HomeCommand(plugin, sqlSession, message, player, huskHomesAPIHook).onCommand(sender, command, label, args);
                }
            } else {
                if (args.length == 1 && args[0].equalsIgnoreCase("home")) {
                    return new HomeCommand(plugin, sqlSession, message, player, huskHomesAPIHook).onCommand(sender, command, label, args);
                } else if (args.length == 2 && args[0].equalsIgnoreCase("import")) {
                    return new ImportCommand(plugin, sqlSession, message, player, worldGuardSetting).onCommand(sender, command, label, args);
                } else if (args.length == 2 && args[0].equalsIgnoreCase("add")) {
                    return new GrantPermissionCommand(plugin, sqlSession, message, player, worldGuardSetting).onCommand(sender, command, label, args);
                } else if (args.length == 1 && args[0].equalsIgnoreCase("tpset")) {
                    return new TpSetCommand(plugin, sqlSession, message, player).onCommand(sender, command, label, args);
                }
            }
        }
        return false;
    }
}
